package com.tian.earth.utils

import com.tian.earth.constant.PreferenceConstants
import com.tian.earth.entity.User

/**
 * @author tiansongtao, Email:769001510@qq.com
 * @date 19-6-26 下午5:37
 */
class UserCenter private constructor(){
    companion object {
        val INSTANCE = Holder.single
    }

    private object Holder {
        val single = UserCenter()
    }


    fun saveUser(user: User) {
        PreferencesUtil.INSTANCE.putIntValue(PreferenceConstants.id, user.id)
        PreferencesUtil.INSTANCE.putStringValue(PreferenceConstants.name, user.name)
        PreferencesUtil.INSTANCE.putStringValue(PreferenceConstants.phone, user.phone)
        PreferencesUtil.INSTANCE.putStringValue(PreferenceConstants.password, user.password)
        PreferencesUtil.INSTANCE.putIntValue(PreferenceConstants.role, user.role)
    }

    fun getUer():User {
        val user = User()
        user.id = PreferencesUtil.INSTANCE.getIntValue(PreferenceConstants.id)
        user.name = PreferencesUtil.INSTANCE.getStringValue(PreferenceConstants.name)
        user.phone = PreferencesUtil.INSTANCE.getStringValue(PreferenceConstants.phone)
        user.password = PreferencesUtil.INSTANCE.getStringValue(PreferenceConstants.password)
        user.role = PreferencesUtil.INSTANCE.getIntValue(PreferenceConstants.role)
        return user
    }

    fun isLogin():Boolean {
        return PreferencesUtil.INSTANCE.getIntValue(PreferenceConstants.id) != -1
    }

}